package cz.muni.fi.pb138.videoweb2013.servlets;

import cz.muni.fi.pb138.videoweb2013.backend.Category;
import cz.muni.fi.pb138.videoweb2013.backend.Medium;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManager;
import cz.muni.fi.pb138.videoweb2013.backend.MediumManagerImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

/**
 * creating new medium action
 * 
 * @author Filip
 */
@UrlBinding("/action/NewMediumContinue.jsp")
public class NewMediumActionBean implements ActionBean {

    private ActionBeanContext context;
    private String platform;
    private static String category;
    private static List<String> categoryHeader = new ArrayList<String>();
    private static List<Category> categories = new ArrayList<Category>();
    private static List<String> textFields = new ArrayList<String>();
    private String finished;

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public String getPlatform() {
        return platform;
    }

    public void setPlatform(String platforma) {
        this.platform = platforma;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String categoryNamee) {
        this.category = categoryNamee;
    }

    public List<String> getCategoryHeader() {
        return categoryHeader;
    }

    public void setCategoryHeader(List<String> categoryHeader) {
        this.categoryHeader = categoryHeader;
    }

    public String getFinished() {
        return finished;
    }

    public void setFinished(String finished) {
        this.finished = finished;
    }

    public List<String> getTextFields() {
        return textFields;
    }

    public void setTextFields(List<String> textFields) {
        this.textFields = textFields;
    }

    /**
     * print header of medium selected by user
     * 
     * @return forward resolution to page NewMediumContinue
     */
    @DefaultHandler
    public Resolution newMedium() {
        categoryHeader.clear();
        categories.clear();
        category = platform;
        MediumManager mmi = new MediumManagerImpl();
        categories.addAll(mmi.getCategories());
        for (int i = 0; i < categories.size(); i++) {
            if (categories.get(i).getCategoryName().equals(category)) {
                categoryHeader.addAll(categories.get(i).getHeader());
            }
        }
        return new ForwardResolution("/NewMediumContinue.jsp");
    }

    /**
     * adds new medium to database with values selected by user
     * 
     * @return forward resolution to page NewMediumContinue
     */
    public Resolution submitMedium() {
        MediumManager mm = new MediumManagerImpl();
        Map<String, String> mp = new HashMap<String, String>();
        for (int i = 0; i < textFields.size(); i++) {
            mp.put(categoryHeader.get(i), textFields.get(i));
        }
        mm.addRecord(new Medium(mp, category, category.replaceAll("\\s", ""), categoryHeader));
        textFields.clear();
        finished = "Medium bylo přidáno.";
        return new ForwardResolution("/NewMediumContinue.jsp");
    }
}
